
export function encrypt(password){
  // 设置公私钥
  const publicKey=`-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwGd25HI1U4CtgKWUtrCUQZo86
  +eKDWwHT8ETlzTE9ioWysYlPWfD73zv6gpAVI3mTH8W/zQKVH1BAL8qaLns/kHQh
  kYyYL5EhiLm6aGIhX1wfUIB6kUScTmhdNlT4Ij7AkmlOAyzmcYS/Iby3xGfMvVty
  7okR3Lc+j4yvOp14WQIDAQAB
  -----END PUBLIC KEY-----`
  const encrypt=new JSEncrypt()
  encrypt.setPublicKey(publicKey)
  let data = encrypt.encrypt(password)
  return data
}

export function max(num) {
  let data = 0;
  switch (Math.abs(Math.ceil(num)).toString().length) {
    case 1:
      data = Math.ceil(num) + 1;
      break;
    case 2:
      if((Math.ceil(num)%10>=0&&Math.ceil(num)%10<5)||(Math.ceil(num)%10<-5)){
        data = Math.floor((Math.ceil(num) + 10) / 10) * 10-5;
        break
      }
      data = Math.floor((Math.ceil(num) + 10) / 10) * 10;
      break;
    case 3:
      if((Math.ceil(num)%100>=0&&Math.ceil(num)%100<50)||(Math.ceil(num)%100<-50)){
        data = Math.floor((Math.ceil(num) + 100) / 100) * 100-50;
        break
      }
      data = Math.floor((Math.ceil(num) + 100) / 100) * 100;
      break;
    case 4:
      if((Math.ceil(num)%1000>=0&&Math.ceil(num)%1000<500)||(Math.ceil(num)%1000<-500)){
        data = Math.floor((Math.ceil(num) + 1000) / 1000) * 1000-500;
        break
      }
      data = Math.floor((Math.ceil(num) + 1000) / 1000) * 1000;
      break;
    case 5:
      if((Math.ceil(num)%10000>=0&&Math.ceil(num)%10000<5000)||(Math.ceil(num)%10000<-5000)){
        data = Math.floor((Math.ceil(num) + 10000) / 10000) * 10000-5000;
        break
      }
      data = Math.floor((Math.ceil(num) + 10000) / 10000) * 10000;
      break;
    case 6:
      if((Math.ceil(num)%100000>=0&&Math.ceil(num)%100000<50000)||(Math.ceil(num)%100000<-50000)){
        data = Math.floor((Math.ceil(num) + 100000) / 100000) * 100000-50000;
        break
      }
      data = Math.floor((Math.ceil(num) + 100000) / 100000) * 100000;
      break;
      case 7:
        if((Math.ceil(num)%1000000>=0&&Math.ceil(num)%1000000<500000)||(Math.ceil(num)%1000000<-500000)){
          data = Math.floor((Math.ceil(num) + 1000000) / 1000000) * 1000000-500000;
          break
        }
        data = Math.floor((Math.ceil(num) + 1000000) / 1000000) * 1000000;
        break;  
  }
  return data;
}


export function min(num) {
  let data = 0;
  switch (Math.abs(Math.floor(num)).toString().length) {
    case 1:
      data = Math.floor(num) == 0 && num >= 0 ? 0 : Math.floor(num) - 1;
      break;
    case 2:
      if((Math.ceil(num)%10>5)||(Math.ceil(num)%10>-5&&Math.ceil(num)%10<=0)){
        data = Math.ceil((Math.ceil(num) - 10) / 10) * 10+5;
        break
      }
      data = Math.ceil((Math.floor(num) - 10) / 10) * 10;
      break;
    case 3:
      if((Math.ceil(num)%100>50)||(Math.ceil(num)%100>-50&&Math.ceil(num)%100<=0)){
        data = Math.ceil((Math.ceil(num) - 100) / 100) * 100+50;
        break
      }
      data = Math.ceil((Math.floor(num) - 100) / 100) * 100;
      break;
    case 4:
      if((Math.ceil(num)%1000>500)||(Math.ceil(num)%1000>-500&&Math.ceil(num)%1000<=0)){
        data = Math.ceil((Math.ceil(num) - 1000) / 1000) * 1000+500;
        break
      }
      data = Math.ceil((Math.floor(num) - 1000) / 1000) * 1000;
      break;
    case 5:
      if((Math.ceil(num)%10000>5000)||(Math.ceil(num)%10000>-5000&&Math.ceil(num)%10000<=0)){
        data = Math.ceil((Math.ceil(num) - 10000) / 10000) * 10000+5000;
        break
      }
      data = Math.ceil((Math.floor(num) - 10000) / 10000) * 10000;
      break;
    case 6:
      if((Math.ceil(num)%100000>50000)||(Math.ceil(num)%100000>-50000&&Math.ceil(num)%100000<=0)){
        data = Math.ceil((Math.ceil(num) - 100000) / 100000) * 100000+50000;
        break
      }
      data = Math.ceil((Math.floor(num) - 100000) / 100000) * 100000;
      break;
      case 7:
        if((Math.ceil(num)%1000000>500000)||(Math.ceil(num)%1000000>-500000&&Math.ceil(num)%1000000<=0)){
          data = Math.ceil((Math.ceil(num) - 1000000) / 1000000) * 1000000+500000;
          break
        }
        data = Math.ceil((Math.floor(num) - 1000000) / 1000000) * 1000000;
        break;  
  }
  return data;
}


export function isWeiXin() {
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  if (!time) {
    return ''
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') {
      return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}